import { ref } from 'vue'
import { defineStore } from 'pinia'
import type { DoctorProfile } from '@/types/doctor'

export interface DoctorState {
  doctorId: string | number
  name: string
  title: string
  department: string
  hospital: string
  avatar: string
  phone: string
  email: string
  sex: string
  birthday: string
  introduction: string
  goodAt: string
  [key: string]: any
}

export const useDoctorStore = defineStore(
  'doctor',
  () => {
    // 医生基本信息
    const doctorId = ref<string | number>('')
    const name = ref('')
    const title = ref('')
    const department = ref('')
    const hospital = ref('')
    const avatar = ref('')
    const phone = ref('')
    const email = ref('')
    const sex = ref('')
    const birthday = ref('')
    const introduction = ref('')
    const goodAt = ref('')

    // 设置医生ID
    function setDoctorId(id: string | number) {
      doctorId.value = id
    }

    // 设置医生姓名
    function setName(doctorName: string) {
      name.value = doctorName
    }

    // 设置职称
    function setTitle(doctorTitle: string) {
      title.value = doctorTitle
    }

    // 设置科室
    function setDepartment(doctorDepartment: string) {
      department.value = doctorDepartment
    }

    // 设置医院
    function setHospital(doctorHospital: string) {
      hospital.value = doctorHospital
    }

    // 设置头像
    function setAvatar(url: string) {
      avatar.value = url
    }

    // 设置医生信息
    function setDoctorInfo(doctorInfo: Partial<DoctorState>) {
      if (doctorInfo.doctorId) doctorId.value = doctorInfo.doctorId
      if (doctorInfo.name) name.value = doctorInfo.name
      if (doctorInfo.title) title.value = doctorInfo.title
      if (doctorInfo.department) department.value = doctorInfo.department
      if (doctorInfo.hospital) hospital.value = doctorInfo.hospital
      if (doctorInfo.avatar) avatar.value = doctorInfo.avatar
      if (doctorInfo.phone) phone.value = doctorInfo.phone
      if (doctorInfo.email) email.value = doctorInfo.email
      if (doctorInfo.sex) sex.value = doctorInfo.sex
      if (doctorInfo.birthday) birthday.value = doctorInfo.birthday
      if (doctorInfo.introduction) introduction.value = doctorInfo.introduction
      if (doctorInfo.goodAt) goodAt.value = doctorInfo.goodAt
    }

    // 从API响应设置医生信息
    function setDoctorProfileFromAPI(profile: DoctorProfile) {
      name.value = profile.name
      phone.value = profile.phone
      department.value = profile.department
      email.value = profile.email
      avatar.value = profile.avatar
      sex.value = profile.sex
      birthday.value = profile.birthday
      introduction.value = profile.introduction
      title.value = profile.title
      goodAt.value = profile.goodAt
    }

    // 清除医生信息
    function clearDoctorInfo() {
      doctorId.value = ''
      name.value = ''
      title.value = ''
      department.value = ''
      hospital.value = ''
      avatar.value = ''
      phone.value = ''
      email.value = ''
      sex.value = ''
      birthday.value = ''
      introduction.value = ''
      goodAt.value = ''
    }

    return {
      doctorId,
      name,
      title,
      department,
      hospital,
      avatar,
      phone,
      email,
      sex,
      birthday,
      introduction,
      goodAt,
      setDoctorId,
      setName,
      setTitle,
      setDepartment,
      setHospital,
      setAvatar,
      setDoctorInfo,
      setDoctorProfileFromAPI,
      clearDoctorInfo
    }
  },
  {
    persist: true
  }
)
